package com.example.demo.admin.controller;/**
 * Created by kete on 2017-10-24.
 */

import com.example.demo.admin.client.MenuServiceClient;
import com.example.demo.admin.core.shiro.ShiroKit;
import com.example.demo.admin.service.MenuService;
import com.example.demo.admin.vo.UserIndexInputVo;
import com.example.demo.admin.vo.UserIndexOutputVo;
import com.example.demo.common.vo.ShiroUser;
import com.example.demo.core.controller.BaseController;
import com.example.demo.core.log.LogManager;
import com.example.demo.core.log.factory.LogTaskFactory;
import com.example.demo.core.util.UserUtil;
import com.example.demo.core.util.WebUtil;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

import static com.example.demo.core.support.HttpKit.getIp;

/**
 * 作者：kete
 * 创建时间：2017-10-24 13:50
 * 功能描述：
 * 版本：
 */
@Controller
public class LoginController extends BaseController {

    @Autowired
    MenuServiceClient menuService;

    /**
     * 跳转到主页
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model, UserIndexInputVo inputVo) {
        //获取菜单列表
        List<Integer> roleList = ShiroKit.getUser().getRoleList();
        if(roleList == null || roleList.size() == 0){
            ShiroKit.getSubject().logout();
            model.addAttribute("tips", "该用户没有角色，无法登陆");
            return "/login.html";
        }

        inputVo.setRoleIds(roleList);

        UserIndexOutputVo outputVo = menuService.getMenuTitle(inputVo);

        model.addAttribute("titles", outputVo.getMenus());

        return "/index.html";
    }


    /**
     * 跳转到登录页面
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        if (ShiroKit.isAuthenticated() || ShiroKit.getUser() != null) {
            return REDIRECT + "/";
        } else {
            return "/login.html";
        }
    }

    /**
     * 点击登录执行的动作
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginVali(String username,String password) {

        Subject currentUser = ShiroKit.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password.toCharArray());
        token.setRememberMe(true);

        currentUser.login(token);

        ShiroUser shiroUser = ShiroKit.getUser();
        WebUtil.getSession().setAttribute("shiroUser", shiroUser);
        WebUtil.getSession().setAttribute("username", shiroUser.getAccount());

        ShiroKit.getSession().setAttribute("sessionFlag",true);
        String accessToken = UserUtil.createAccessToken();
        UserUtil.setUser(shiroUser,accessToken);
        UserUtil.addCookie(UserUtil.accessToken, accessToken);

        LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), getIp()));

        return REDIRECT + "/";
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logOut() {

        ShiroKit.getSubject().logout();
        return REDIRECT + "/login";
    }
}
